#include <iostream>
#include <fstream>

#include "ros/ros.h"
#include "demo/Camera.h"

void callback(const demo::Camera::ConstPtr &msg) {
    ROS_INFO("width: %d, height: %d, size: %ld", msg.get()->width, msg.get()->height, msg.get()->data.size());
    const std::string path = "./src/demo/data/client.png";
    std::ofstream file(path, std::ios::binary);
    if (!file.is_open()) {
        ROS_ERROR("Failed to open file: %s", path.c_str());
        return;
    }
    file.write(reinterpret_cast<const char *>(msg.get()->data.data()), msg.get()->data.size());
    file.close();
}

int main(int argc, char* argv[]) {
    std::cout << "Hello Client!!!" << std::endl;
    ros::init(argc, argv, "client");
    ros::NodeHandle nodeHandle;
    const std::string topicName = "demo9";
    uint32_t queueSize = 1000;
    const ros::Subscriber &subcriber = nodeHandle.subscribe<demo::Camera>(topicName, queueSize, callback);
    ros::spin();

    return EXIT_SUCCESS;
}